<?php
namespace Home\Model;
use Think\Model;
// +----------------------------------------------------------------------
// | Date:2016年8月12日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class PostsModel extends Model{
    

    
    /**
     * 根据分类，获取文章列表信息
     * @param unknown $catName 分类名称
     * @param unknown $page
     * @param unknown $limit
     * @param unknown $istop   是否置顶
     * @return unknown|NULL
     * date:2016年8月12日
     * author: EK_熊
     */
    function getList($catName,$page=1,$limit=20){
        $postId = $this->getPostId($catName); 
        
        if (!$postId) return null;
        
        $listMap['post_status'] = '1';
        $listMap['id'] = ['in',$postId];
        
        $list = $this->where($listMap) 
                    ->join('__TERM_RELATIONSHIPS__ ON __TERM_RELATIONSHIPS__.object_id = __POSTS__.id')
                    ->field("id,post_title,DATE_FORMAT(post_modified,'%Y/%m/%d') as post_modified,smeta,term_label,post_excerpt,listorder,post_brand")
                    ->group('id')
                    ->order('listorder ASC ,post_date DESC')
                    ->page($page,$limit)
                    ->select();
        if ($list) {
            
            $list = $this->format_smeta($list);
            return $list;
        }
        return false;
        
    }
    
    
   /**
    * 搜索文章标题，模糊查询
    * @param unknown $key
    * @return \Home\Model\unknown|boolean
    * date:2016年10月11日
    * author: EK_熊
    */ 
   function search($key){
        if (empty($key)) return false;
        $where['post_status'] = 1;
        $where['_string'] = "(post_title like '%$key%')  OR ( post_brand like '%$key%')";
        $list = $this
            ->where($where)
            ->field("id,post_title,DATE_FORMAT(post_modified,'%Y/%m/%d') as post_modified,smeta,post_brand")
            ->select();
        if ($list) {
        
            $list = $this->format_smeta($list);
            return $list;
        }
        return false;
              
        
    } 
    
    /**
     * 根据分类，获取postid
     * @param unknown $catName
     * date:2016年9月22日
     * author: EK_熊
     */
    function getPostId($catName){
        if (is_array($catName)) {
            
            $parentId = M('terms')->where(['name_en'=>['in',$catName],'status'=>1])->getField('term_id',true);//获取参数传进来标签的主键值
        }else{
            
            $parentId = M('terms')->where(['name_en'=>$catName,'status'=>1])->getField('term_id');//获取参数传进来标签的主键值
        }
        $sonId = M('terms')->where(['parent'=>['in',$parentId],'status'=>1])->getField('term_id',true);
        if ($sonId) {
           $termId = $sonId;
        }else{
           $termId =$parentId; 
        }
        $postId = M('term_relationships')->where(['term_id'=>['in',$termId],'status'=>1])->getField('object_id',true);
        return $postId ? array_unique($postId) : false;
        
        
    }
    


    
    /**
     * 格式化解析 smeta拓展字段
     * @param array $list
     * @return unknown
     * date:2016年8月12日
     * author: EK_熊
     */
    function format_smeta($list=array()){
        
        if ($list) {
            foreach ($list as $k=>$v){
                $smeta = json_decode($v['smeta'],true);
                $list[$k]['cover'] = $smeta['thumb'];
                unset($list[$k]['smeta']);
            }
        }
        return $list;
    }
    

    
    /**
     * 获取文章详情
     * @param unknown $id
     * @param unknown $paging   true/false 是否有分页
     * @return mixed|boolean|NULL|string|unknown|object
     * date:2016年8月12日
     * author: EK_熊
     */
    function detail($id,$paging=false){
        $detail = $this->where(['post_status'=>'1','id'=>$id])
                        ->join('__TERM_RELATIONSHIPS__ ON __TERM_RELATIONSHIPS__.object_id = __POSTS__.id')
                        ->field('id,post_title,post_content,smeta,post_excerpt,post_keywords,post_brand,video,listorder')
                        ->find();
        $listorder = $detail['listorder'];
        if ($detail) {
            $smeta = json_decode($detail['smeta'],true);
            
            if ($smeta){
                   
                $banner = $smeta['photo'][0];
                unset($smeta['photo'][0]);
                $detail['photo']  = $smeta['photo'][1];     
                $detail['banner'] = $banner;
                unset($detail['smeta']);
            }
            
            if ($paging) {
                $next = M('term_relationships')->where(['status'=>1,'_string'=> "listorder>$listorder"])
                    ->order('listorder asc,object_id asc')
                    ->getField('object_id');//下一篇
                 
                $prve = M('term_relationships')->where(['status'=>1,'_string'=> "listorder<$listorder"])
                    ->order('listorder desc,object_id desc')
                    ->getField('object_id');//上一篇
                $detail['next'] = $next ? $next : false;
                $detail['prve'] = $prve ? $prve : false;
            }
            
            //增加点击数
            M('posts')->where(['id'=>$detail['object_id']])->setInc('post_hits');
        }
        return $detail;
    }
    
    
    function detail_page(){
        
    }
    
    
    /**
     * 获取分类标签，根据父标签
     * @param unknown $parentName
     * date:2016年9月26日
     * author: EK_熊
     */
    function getTermByParent($parentLabel){
        $parent = M('terms')->where(['name_en'=>$parentLabel])->getField('term_id');
        $ret = M('terms')->where(['parent'=>$parent])->getField('name_en,name',true);
        return $ret;
        
    }
    
}